package com.study.servlet.session.prac01;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* @Name: LoginSessionServlet
* @Description: Session应用：防止表单重复提交
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-3-3 下午3:27:00 
* @Version: V1.0
 */
public class LoginSessionServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8") ;
		PrintWriter out = response.getWriter() ;
		//获取页面请求参数
		request.setCharacterEncoding("UTF-8") ; //设置请求参数编码，适用于POST提交
		String TOKEN = request.getParameter("token") ;
		HttpSession session = request.getSession() ;
		String STOKEN = (String) session.getAttribute("TOKEN") ;
		//如果页面提交的TOKEN与Session中的TOKEN相等，说明为第一次提交，允许提交，并清空Session中的TOKEN
		if(TOKEN.equals(STOKEN)) {
			session.removeAttribute("TOKEN") ;
			out.write("提交成功！！！") ;
		} else {
			out.write("对不起，不能重复提交！！！") ;
		}
		out.close() ;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
